local curl = require "lcurl.safe"
local json = require "cjson.safe"
script_info = {
	["title"] = "KD算法通道",
	["version"] = "0.0.0",
	["color"] = "#4682B4",
	["description"] = "Kinhdown提供",
}

function onInitTask(task, user, file)
    if task:getType() ~= TASK_TYPE_SHARE_BAIDU then
		task:setError(-1,"请用分享下载或者打开分享链接")
		--[[
		local url = "http://127.0.0.1:8989/yjx1?path="..pd.urlEncode(file.path)
		local BDUSS = user:getBDUSS()
		local header = { "User-Agent: netdisk;2.2.51.6;netdisk;10.0.63;PC;android-android;QTP/1.0.32.2" }
		table.insert(header, "Cookie: BDUSS="..BDUSS.."SignText")
		local data = ""
		local c = curl.easy{
		url = url,
		followlocation = 1,
		httpheader = header,
		timeout = 200,
		proxy = pd.getProxy(),
		writefunction = function(buffer)
			data = data .. buffer
			return #buffer
		end,
		}
		local _, e = c:perform()
		c:close()
		
		if e then
			task:setError(-1,"链接至本地服务器失败,检查8989端口")
			return true
		end

		local isban = string.find(data, "issuecdn")
		if isban ~= nil then 
			task:setError(-1,"违禁文件，已被禁止下载")
			return true
		end
		pd.logInfo(data)
		local j = json.decode(data)
		if j == nil then
			task:setError(-1,"链接请求失败,可能已经黑号")
			return true
		end
		local sign = 0 
		local message = {}
		for i, w in ipairs(j.server) do
			table.insert(message, j.server[i])
			if j.server[i]~="qdall01.baidupcs.com" then 
				sign=1
			end
		end
		local num = 1
		
		local downloadURL = "https://"..j.server[num]..j.path
		if sign==0 then
			downloadURL = j.path1
		else
			local num = pd.choice(message, 1, "选择下载接口")
			downloadURL = "https://"..j.server[num]..j.path
		end
		
		pd.logInfo("下载地址："..downloadURL)
		task:setUris(downloadURL)
		task:setOptions("user-agent", "netdisk;P2SP;2.2.60.26")
		task:setIcon("icon/tips_warn.png", "未知状态")
		task:setOptions("piece-length", "1M")
		--task:setOptions("split", "64")
		task:setOptions("allow-piece-length-change", "true")
		task:setOptions("enable-http-pipelining", "true")
		]]
        return true
    end
	if task:getType() == TASK_TYPE_SHARE_BAIDU then
		--task:setError(-1,"请用登录账号下载盘内文件")


		local dlinklink = file.dlink
		if user ~= nil then
			bdss = user:getCookie()
			else
			bdss = ""
		end	
		
		--local url = "https://admir.xyz/blog/ad/yjx.php?ua="..ua.."&data="..pd.base64Encode(string.gsub(string.gsub(file.dlink, "https://d.pcs.baidu.com/file/", "&path="), "?fid", "&fid").."&clienttype=8&channel=00000000000000000000000000000000")
		
		local url = "http://127.0.0.1/yjx?dlink="..dlinklink
		pd.logInfo(url)
		pd.logInfo(dlinklink)
		local data = ""
		local header = { "User-Agent: netdisk;P2SP;2.2.60.26" }
		pd.logInfo(bdss)
		if bdss ~= "" then
			table.insert(header, "Cookie: "..bdss.."SignText")
		end
		
		local c = curl.easy{
			url = url,
			followlocation = 1,
			httpheader = header,
			timeout = 10,
			proxy = pd.getProxy(),
			writefunction = function(buffer)
				data = data .. buffer
				return #buffer
			end,
		}
		local _, e = c:perform()
		c:close()
		if e then
			task:setError(-1,"链接至本地服务器失败,检查8989端口")
			return true
		end
		pd.logInfo(data)
		local j = json.decode(data)
		if j == nil then
			
			task:setError(-1,"请求错误，链接云解析失败")
			return true
		end
		
		if string.find(data, "issuecdn") ~= nil then 
			task:setError(-1,"违禁文件，已被禁止下载")
			return true
		end

		local j = json.decode(data)
			local j = json.decode(data)

		
		--local num = pd.choice(message, 1, "选择下载接口")
		--local downloadURL = "https://"..j.server[num]..j.path
		--downloadURL = string.gsub(downloadURL,"http","https")
		pd.logInfo("下载地址："..j.url)
		task:setUris(j.url)
		--task:setOptions("user-agent", "netdisk;P2SP;3.0.0.3")
		--pd.logInfo(ua)
		--task:setOptions("header", "Range:bytes=0-0")
		task:setOptions("header", j.header)
		
		task:setOptions("user-agent", j.ua)
		--task:setOptions("header", "Cookie: "..user:getCookie())
		task:setOptions("piece-length", "1M")
		task:setOptions("max-connection-per-server", j.split)
		task:setOptions("split", j.split)
		task:setOptions("piece-length", "1M")
		return true
	end
end